#include "PrintTool.h"

PrintTool::PrintTool(QObject *parent)
    : QObject{parent}
{

}

PrintTool::~PrintTool()
{

}


void PrintTool::printEvent(QPixmap printImg){
    this->printImg = printImg;

    QPrinter printer(QPrinter::HighResolution);
    //自定义纸张大小
    printer.setPageSize(QPrinter::Custom);
    printer.setPaperSize(QSizeF(printImg.height(), printImg.width()),
                               QPrinter::Point);
    QPrintPreviewDialog preview(&printer, this);
    preview.setMinimumSize(1000,600);
    connect(&preview, SIGNAL(paintRequested(QPrinter*)), SLOT(printPreviewSlot(QPrinter*)));
    preview.exec ();
}

void PrintTool::printPreviewSlot(QPrinter * printerPixmap){
    printerPixmap->setOrientation(QPrinter::Landscape);
    //获取界面的图片
    QPixmap pixmap = this->printImg;
    QPainter painterPixmap(this);
    painterPixmap.begin(printerPixmap);
    QRect rect = painterPixmap.viewport();
    int x = rect.width() / pixmap.width();
    int y = rect.height() / pixmap.height();
    painterPixmap.scale(x, y);
    painterPixmap.draw(0, 0, pixmap);
    painterPixmap.end();
}

